package com.qk.management.controller;


import com.qk.common.Result;
import com.qk.common.util.AliYunOSSOperators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;

/**
 * @author: Enid
 * @date: 2025/10/9 10:10
 * @description: 文件处理控制器
 */
@Slf4j
@RestController
public class FileHandlerController {
    @Autowired
    private AliYunOSSOperators oss;

    @PostMapping("/upload")
    public Result uploadFile(@RequestParam("image") MultipartFile image) {
        log.info("上传的文件:{}", image);
        try {
            Objects.requireNonNull(image, "上传的文件不能为空");
            Objects.requireNonNull(image.getOriginalFilename(), "上传的文件名不能为空");


            String originalFilename = image.getOriginalFilename();
            int i = originalFilename.lastIndexOf(".");//索引
            String suffix = originalFilename.substring(i);
            String objectName = UUID.randomUUID() + suffix;
            String url = oss.upload(image.getBytes(), objectName);
            return Result.success(url);
        } catch (Exception e) {
            log.error("上传文件失败{}", e.getMessage());
            return Result.error("上传文件失败");
        }
    }
}
